package org.aceor.mddal.gateway.merger.common;

import com.google.common.base.Strings;

/**
 * Created by lxue on 16/4/16.
 */
public final class ResultSetQueryIndex {

    private int queryIndex;

    private String queryName;

    public ResultSetQueryIndex(final Object queryParam) {
        if (queryParam instanceof Integer) {
            queryIndex = (int) queryParam;
        } else if (queryParam instanceof String) {
            queryName = queryParam.toString();
        } else {
            throw new IllegalArgumentException(queryParam.getClass().getName());
        }
    }

    public void checkValid() {
        if (Strings.isNullOrEmpty(queryName)
                && queryIndex <= 0){
            throw new IllegalArgumentException("queryIndex :" + queryIndex+", queryName :" + queryName);
        }
    }
    /**
     * 获取是否通过序号查询.
     *
     * @return 通过序号查询返回true, 通过名称查询返回false
     */
    public boolean isQueryByIndex() {
        return Strings.isNullOrEmpty(queryName);
    }

    /**
     * 忽略类型获取查询索引.
     *
     * @return 查询索引
     */
    public Object getRawQueryIndex() {
        return isQueryByIndex() ? queryIndex : queryName;
    }

    public Integer getQueryIndex() {
        return queryIndex;
    }

    public String getQueryName() {
        return queryName;
    }
}
